package com.tabview.difficultpoint;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.ui.BaseActivity;
import com.xudow.app.util.Maps;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static com.xudow.app.util.LogUtils.LOGE;

/**
 * 作者：zhouXJ on 2016/3/29 17:28
 * 课程重难点
 */
public class CourseDifficultPointsActivity extends BaseActivity {

    private TabPagerView tabPagerView;
    private String studentId;
    private CourseDifficultPointsTitleTask task;
    private List<Points> pointsList;
    private List<String> stringList = new ArrayList<String>();
    private List<Fragment> flist = new ArrayList<Fragment>();
    private RelativeLayout class_course_difficult_points_empty_rl;

    Handler pointHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (CourseDifficultPointsTitleTask.SUCCESS == msg.what) {
                Points[] svoArr = (Points[]) msg.getData().getSerializable("schedules");
                if (null == svoArr || svoArr.length <= 0) {
                    class_course_difficult_points_empty_rl.setVisibility(View.VISIBLE);
                    return;
                }

                pointsList = new ArrayList<Points>();
                pointsList.addAll(Arrays.asList(svoArr));

                for (Points p : pointsList) {
                    stringList.add(p.getClassname());
                    CourseDifficultPointsFragment c = new CourseDifficultPointsFragment();
                    c.setStudentid(studentId);
                    c.setTitle(p.getClassname());
                    c.setAgencyid(p.getAgencyid());
                    c.setOrderinfooaid(p.getOrderinfooaid());
                    flist.add(c);
                }

                tabPagerView.addAllPage(CourseDifficultPointsActivity.this, stringList, flist);

            } else if (CourseDifficultPointsTitleTask.ERROR == msg.what) {
                XApplication.hideLoadingDialog();
                Toast.makeText(CourseDifficultPointsActivity.this, "请求失败，稍后重试", Toast.LENGTH_SHORT).show();
            }


        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_view_hard_layout);
        tabPagerView = (TabPagerView) findViewById(R.id.tab_pager_view);
        class_course_difficult_points_empty_rl = (RelativeLayout) findViewById(R.id.class_course_difficult_points_empty_rl);

        studentId = getIntent().getStringExtra("studentId");
        LOGE("zxj", "studentId = " + studentId);
        if (studentId == null) {
            class_course_difficult_points_empty_rl.setVisibility(View.VISIBLE);
            tabPagerView.setVisibility(View.GONE);
        } else {
            class_course_difficult_points_empty_rl.setVisibility(View.GONE);
            tabPagerView.setVisibility(View.VISIBLE);

            HashMap<String, String> params = Maps.newHashMap();
            params.put("studentid", studentId);

            XApplication.showLodingDialog("正在加载", this);
            task = new CourseDifficultPointsTitleTask(this, pointHandler);
            addTask(task);
            task.execute(params);
        }
    }

    public void onBackBtnClick(View view) {
        finish();
    }

}
